package com.itheima.safe.util;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author：shenkunlin
 * @date： 2021/7/26 2:56
 * @Description：
 ***/
public class MatchUtil {

    /**
     * 过滤
     */
    public static List<String> filter(String regex, String data) {
        //sb存放正则匹配的结果
        List<String> list = new ArrayList<String>();
        //编译正则字符串
        Pattern p = Pattern.compile(regex);
        //利用正则去匹配
        Matcher matcher = p.matcher(data);
        //如果找到了我们正则里要的东西
        while (matcher.find()) {
            //保存到sb中，"\r\n"表示找到一个放一行，就是换行
            list.add(matcher.group());
        }
        return list;
    }

    /**
     * 图片过滤
     */
    public static List<String> matchImages(String content){
        String regex = "(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+(gif|jpg|jpeg|png)";
        return filter(regex,content);
    }

    /**
     * 图片过滤
     */
    public static List<String> matchdfsImg(String content){
        String regex = "(https?|ftp|file)://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+(gif|jpg|jpeg|png)";
        List<String> images = filter(regex,content);
        List<String> dfsImages = new ArrayList<String>();
        for (String image : images) {
            if(image.contains("/group1/M00")){
                dfsImages.add(image);
            }
        }
        return dfsImages;
    }

    /**
     * 去掉url地址
     * @param content
     * @return
     */
    public static String replateUrl(String content) {
        return content.replaceAll("http:[a-z\\/\\.0-9]+", "").replace("group1/M00","").replace(".jpg","").replace("M00","").replace("00","").replace("/","");
    }

    public static void main(String[] args) {
        //1. 读取文档
        String data = "[{\"http://192.168.211.136/group1/M00/00/00_gi\":\"image\",\"value\":\"吃饭了没http://192.168.211.136/group1/M00/00/00/wKjTiGD5RbGAVJ8aAAE-IrxiV_o455.jpg没有啊\"},{\"type\":\"text\",\"value\":\"这不是张三丰，这是张柏芝\"},{\"type\":\"image\",\"value\":\"http://192.168.211.136/group1/M00/00/00/wKjTiGD6V7uAB-vCAADNBGFhf9Q724.jpg\"},{\"type\":\"text\",\"value\":\"请在这里输入正文\"}]";
        //2. 正则查找
        List needData = matchImages(data);
        //3. 写到某个文件中
        System.out.println(needData);

        System.out.println(replateUrl(data));
    }
}
